package org.example.ymcc.consumer;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.example.ymcc.constant.Constants;
import org.example.ymcc.constant.MQConstants;
import org.example.ymcc.domain.CourseCollect;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Component
@RocketMQMessageListener(consumerGroup = "OrderPhoneConsumer",
        topic = MQConstants.COURSE_PLACE_AN_ORDER_TOPIC,
        selectorExpression = MQConstants.COURSE_PLACE_AN_ORDER_TAG
)
@Slf4j
public class OrderPhoneConsumer implements RocketMQListener<MessageExt> {

    @Override
    public void onMessage(MessageExt messageExt) {
        try {
            byte[] body = messageExt.getBody();
            String jsonMessage = new String(body, Constants.UTF8);
            JSONObject jsonObject = JSONObject.parseObject(jsonMessage);
            //假设发送短信
            jsonObject.get("userId");//根据userId获取手机号
            String title = jsonObject.get("message").toString();//title
            String orderSn = jsonObject.get("orderSn").toString();//orderSn
            System.out.println("你已经成功下单："+title+":"+"订单编号是："+orderSn);
            //发邮件
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }
}
